/**
 * @author 0283000115
 * @date 2017年9月23日-下午2:23:28
 */
package com.zte.claa.inficombo.csa.app.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 0283000115
 *
 */
public class PropertiesUtils {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
	
	// 配置文件列表.
	private static final String[] propsFiles = {
		"init.properties",
		"redis.properties",
		"url.properties"
	};
	
	private static boolean isInit = false;
	private static Properties pps = null;
	
	/**
	 * 
	 * @throws Exception
	 */
	private static void init() throws Exception {
		String pathPrefix = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); 
		pps = new Properties();
		for (String filePath : propsFiles) {			
			InputStream in = new BufferedInputStream(new FileInputStream(pathPrefix+filePath));
			try{
				pps.load(in);
				logger.info("load properties file ok: "+filePath);
			}catch(Exception e){
				logger.error(e.getMessage());
			}finally {
				if(in != null){
					in.close();
					in = null;
				}
			}
		}
	}
	
	/**
	 * 防止实例化
	 */
	private PropertiesUtils(){
		
	}
	
	/**
	 * 
	 * @param propKey
	 * @param defaultVal
	 * @return
	 */	
	public static String getProperties(String propKey, String defaultVal){
		synchronized(propsFiles){
			if(!isInit){
				try {
					init();
					isInit = true;
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		if(pps == null){
			return defaultVal;
		}
		return pps.getProperty(propKey, defaultVal);	
	}
}
